const router = require('koa-router')()
const fs = require('fs')
const path = require('path')
router.prefix('/zbf')
const formidable = require('formidable')
const {
    promises: {
        readFile,
        writeFile,
        mkdir,
        unlink,
        readFileSync
    }
} = require('fs');
const CreateSequelize = require('./sequelize')
const {
    DataTypes,
    Op
} = require('sequelize');
const Sequelize = require('sequelize')
let sequelize = new CreateSequelize()
sequelize.init_sequeleze()
const WechatMoments = sequelize.db.define('wechatMoments', {
    user_id: {
        type: DataTypes.STRING,
        comment: '帐号'
    },
    img: {
        type: DataTypes.STRING,
        comment: '图片'
    },
    text: {
        type: DataTypes.STRING,
        comment: '文字'
    },
    video: {
        type: DataTypes.STRING,
        comment: '视频'
    },
    name: {
        type: DataTypes.STRING,
        comment: '姓名'
    },
    time: {
        type: DataTypes.STRING,
        comment: '时间'
    }
}, {
    timestamps: false
})

router.get('/getList', async (ctx, next) => {
    let data = await WechatMoments.findAll({})
    ctx.body = data

})

router.post('/bar', function async(ctx, next) {
    let fileds = ctx.request.body
    let paths = ctx.request.files.file.path
    let biao = {}
    let data = fs.readFileSync(paths)
    if (!fs.existsSync(path.join(__dirname, '../public/img/' + fileds.filename))) {
        fs.mkdirSync(path.join(__dirname, '../public/img/' + fileds.filename))
    }
    let img = fileds.filename + '.jpg[' + fileds.num + ']'
    fs.writeFileSync(path.join(__dirname, '../public/img/' + fileds.filename + '/' + img), data)
    if (fileds.max - 1 == fileds.num) {
        let arr = fs.readdirSync(path.join(__dirname, '../public/img/' + fileds.filename))
        let da
        for (let a = 0; a < arr.length; a++) {
            let i = path.join(__dirname, '../public/img/' + fileds.filename + '/' + arr[a])
            let v = fs.readFileSync(i)
            if (a === 0) {
                da = v
            } else {
                da = Buffer.concat([da, v])
            }
        }
        fs.writeFileSync(path.join(__dirname, '../public/img/' + fileds.filename + '.png'), da)
        for (let a = 0; a < arr.length; a++) {
            let i = path.join(__dirname, '../public/img/' + fileds.filename + '/' + arr[a])
            fs.unlink(i, (err) => {
                if (err) {
                    console.log(err);
                }
            })
            console.log(fs.readdirSync(path.join(__dirname, '../public/img/' + fileds.filename)));
            console.log(a);
            if (arr.length - 1 == a) {
                fs.rmdirSync(path.join(__dirname, '../public/img/' + fileds.filename))
            }
        }
        let time = Date.now()
        WechatMoments.create({
            user_id: fileds.info_id,
            img: 'http://localhost:3030/img/' + fileds.filename + '.png',
            text: fileds.text,
            name: fileds.xing,
            time: time
        })
        biao = {
            user_id: fileds.info_id,
            img: 'http://localhost:3030/img/' + fileds.filename + '.png',
            text: fileds.text,
            name: fileds.xing,
            time: time,
            video: null
        }
    }
    ctx.body = biao
})
router.post('/video', function async(ctx, next) {
    let fileds = ctx.request.body
    let paths = ctx.request.files.file.path
    // console.log(ctx.request.body);
    let biao = {}
    let data = fs.readFileSync(paths)
    if (!fs.existsSync(path.join(__dirname, '../public/img/' + fileds.filename))) {
        fs.mkdirSync(path.join(__dirname, '../public/img/' + fileds.filename))
    }
    let img = fileds.filename + '.mp4[' + fileds.num + ']'
    fs.writeFileSync(path.join(__dirname, '../public/img/' + fileds.filename + '/' + img), data)
    if (fileds.max - 1 == fileds.num) {
        let arr = fs.readdirSync(path.join(__dirname, '../public/img/' + fileds.filename))
        let da
        for (let a = 0; a < arr.length; a++) {
            let i = path.join(__dirname, '../public/img/' + fileds.filename + '/' + arr[a])
            let v = fs.readFileSync(i)
            if (a === 0) {
                da = v
            } else {
                da = Buffer.concat([da, v])
                // console.log(a, arr.length);
                if (arr.length - 1 == a) {
                    fs.writeFileSync(path.join(__dirname, '../public/img/' + fileds.filename + '.mp4'), da)
                }
            }
        }
        // console.log(da);

        for (let a = 0; a < arr.length; a++) {
            let i = path.join(__dirname, '../public/img/' + fileds.filename + '/' + arr[a])
            fs.unlink(i, (err) => {
                if (err) {
                    console.log(err);
                }


            })
        }
        if (fs.readdirSync(path.join(__dirname, '../public/img/' + fileds.filename)).length == 0) {
            fs.rmdirSync(path.join(__dirname, '../public/img/' + fileds.filename))
        }
        biao = 'http://localhost:3030/img/' + fileds.filename + '.mp4'
        let time = Date.now()
        WechatMoments.create({
            user_id: fileds.info_id,
            video: 'http://localhost:3030/img/' + fileds.filename + '.mp4',
            text: fileds.text,
            name: fileds.xing,
            time: time
        })
        biao = {
            user_id: fileds.info_id,
            video: 'http://localhost:3030/img/' + fileds.filename + '.mp4',
            text: fileds.text,
            name: fileds.xing,
            time: time,
            img: null
        }
    }
    ctx.body = biao
})
// router.post('/video', function async (ctx, next) {
//     let fileds = ctx.request.body
//     let paths = ctx.request.files.file.path
//     let biao = {}
//     let data = fs.readFileSync(paths)
//     if (!fs.existsSync(path.join(__dirname, '../public/img/' + fileds.filename))) {
//         fs.mkdirSync(path.join(__dirname, '../public/img/' + fileds.filename))
//     }
//     let img = fileds.filename + '.mp4[' + fileds.num + ']'
//     fs.writeFileSync(path.join(__dirname, '../public/img/' + fileds.filename + '/' + img), data)
//     if (fileds.max - 1 == fileds.num) {
//         let arr = fs.readdirSync(path.join(__dirname, '../public/img/' + fileds.filename))
//         let da
//         for (let a = 0; a < arr.length; a++) {
//             let i = path.join(__dirname, '../public/img/' + fileds.filename + '/' + arr[a])
//             // console.log(a);
//             let v = fs.readFileSync(i)
//             if (a === 0) {
//                 da = v
//             } else {
//                 da = Buffer.concat([da, v])
//             }
//         }
//         fs.writeFileSync(path.join(__dirname, '../public/img/' + fileds.filename + '.mp4'), da)
//         for (let a = 0; a < arr.length; a++) {
//             let i = path.join(__dirname, '../public/img/' + fileds.filename + '/' + arr[a])
//             fs.unlink(i, (err) => {
//                 if (err) {
//                     console.log(err);
//                 }
//                 if(fs.readdirSync(path.join(__dirname, '../public/img/' + fileds.filename)).length==0){
//                     fs.rmdirSync(path.join(__dirname, '../public/img/' + fileds.filename))
//                 }
//             })
//         }

//         // biao = 'http://localhost:3030/img/' + fileds.filename + '.mp4'
//         let time=Date.now()
//         WechatMoments.create({
//             user_id: fileds.info_id,
//             video: 'http://localhost:3030/img/' + fileds.filename + '.mp4',
//             text: fileds.text,
//             name:fileds.xing,
//             time:time
//         })
//         biao={
//             user_id: fileds.info_id,
//             video: 'http://localhost:3030/img/' + fileds.filename + '.mp4',
//             text: fileds.text,
//             name:fileds.xing,
//             time:time
//         }
//     }
//     ctx.body = biao
// })

module.exports = router